package com.uyaer.wheel.display.scene
{
	import com.uyaer.wheel.common.Common;
	import com.uyaer.wheel.tick.TimerManager;
	import com.uyaer.wheel.utils.Utils;
	
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	
	import starling.utils.deg2rad;
	
	/**
	 *加载界面，显示各种提示，或者界面，或者最新资讯 
	 * @author hc
	 * 
	 */	
	public class Loading extends Sprite
	{
		private var _callback:Function;
		
		private var top:Sprite;
		private var bottom:Sprite;
		private var w:Number;
		private var h:Number;
		private var gradientW:Number;
		private var startTime:Number;
		private var speed:Number = 30;
		private var topY:Number;
		private var bottomY:Number;
		private var g:Graphics;
		private var phySp:Sprite;
		private var phyX:Number = 0;
		private var isLineOutOf:Boolean = false;
		private var lineDispearIndex:int = 10;

		private var tree:load_tree;
		
		public function Loading()
		{
			w = Common.W >> 1;
			h = Common.H >> 1;
			gradientW = Common.W * Math.SQRT2;
			layout();
		}
		
		private function layout():void{
			var c1:uint = 0xffffff;
			var c2:uint = 0x46a2f8;
			top = new Sprite();
			bottom = new Sprite();
			addChild(top);
			addChild(bottom);
			bottom.y = h;
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(gradientW,gradientW,deg2rad(45),(1- Math.SQRT2)*w,h-gradientW/2);
			top.graphics.beginGradientFill(GradientType.RADIAL,[c1,c2],[1,1],[0,255],matrix);
			top.graphics.drawRect(0,0,Common.W,h);
			top.graphics.endFill();
			
			matrix.createGradientBox(gradientW,gradientW,deg2rad(-45),(1- Math.SQRT2)*w,-gradientW/2);
			bottom.graphics.beginGradientFill(GradientType.RADIAL,[c1,c2],[1,1],[0,255],matrix);
			bottom.graphics.drawRect(0,0,Common.W,h);
			bottom.graphics.endFill();
			
			top.cacheAsBitmap = true;
			bottom.cacheAsBitmap = true;
			
			phySp = new Sprite();
			g = phySp.graphics;
			addChild(phySp);
			
			tree = new load_tree();
			tree.x = w;
			tree.y = h;
			tree.stop();
		}
		
		/**
		 *0.0-1.0 
		 * @param d
		 * 
		 */		
		public function update(d:Number):void{
		}
		
		public function set callback(value:Function):void{
			_callback = value;
		}
		
		private function reset():void{
		}
		
		public function start():void{
			top.y = -h;
			bottom.y = Common.H;
			topY = top.y;
			bottomY = bottom.y;
			startTime = Utils.now();
			TimerManager.addLoop(closeMe,33);
		}
		
		private function closeMe():void{
			top.y +=speed;
			bottom.y -=speed;
			if(top.y>=0){
				top.y = 0;
				bottom.y = h;
				TimerManager.removeLoop(closeMe);
				isLineOutOf = false;
				phyX = 0;
				TimerManager.addLoop(showPhy,20);
				phySp.filters = [new BlurFilter(),new GlowFilter(0x4eeafc, 0.5, 10, 10, 2, 1, false, false)];
			}
		}
		
		public function stop():void{
			if(tree.parent){
				tree.stop();
				tree.parent.removeChild(tree);
			}
			topY = top.y;
			bottomY = bottom.y;
			startTime = Utils.now();
			TimerManager.addLoop(openMe,33);
		}
		
		private function openMe():void{
			top.y -= speed;
			bottom.y += speed;
			if(top.y <= -h){
				top.y = -h;
				bottom.y = Common.H;
				TimerManager.removeLoop(openMe);
				parent && parent.removeChild(this);
				_callback && _callback();
				this.reset();
			}
		}
		
		private function showPhy():void{
			g.clear();
			if(!isLineOutOf){
				g.moveTo(phyX,h);
				for(var i:int = 0 ; i < 10 ; i ++){
					g.lineStyle(14-i,0xffffff,1);
					g.lineTo(phyX-i*5,h);
				}
				g.lineTo(0,h);
					
				phyX+=12+int(phyX/12);
				if(phyX>Common.W + 1000){
					isLineOutOf = true;
					lineDispearIndex = 20;
				}
			}else{ //慢慢的溶解line
				var offset:Number = w/20*(20-lineDispearIndex);
				g.lineStyle(0.25*lineDispearIndex,0xffffff,1);
				g.moveTo(offset,h);
				g.lineTo(Common.W-offset,h);
				lineDispearIndex--;
				if(lineDispearIndex == 0){
					TimerManager.removeLoop(showPhy);
					TimerManager.addLoop(randomCir,33);
				}
			}
		}
		
		private function randomCir():void{
			g.clear();
			var r:Number = Utils.rangInt(5,40);
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(r<<1,r<<1,0,w-r,h-r);
			g.beginGradientFill(GradientType.RADIAL,[0x58acf9,0x58acf9],[0.65,0],[0,255],matrix);
			g.drawCircle(w,h,r);
			g.endFill();
			lineDispearIndex++;
			if(lineDispearIndex>15){
				addChild(tree);
				tree.gotoAndPlay(1);
				TimerManager.removeLoop(randomCir);
				g.clear();
				phySp.filters = null;
				TimerManager.addDelay(function():void{
					_callback && _callback();
				},1000);
			}
		}
	}
}